<template>
  <div class="Box">
    <div class="box">
      <div class="lef">
        <img src="../assets/img/logins.png" />
      </div>
      <div class="login-container">
        <div class="logo"><img src="../assets/img/logo.png" /></div>
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" prop="name">
            <el-input
              type="tetx"
              v-model="ruleForm.name"
              autocomplete="off"
              placeholder="请输入用户名"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pwd">
            <el-input
              type="text"
              v-model="ruleForm.pwd"
              autocomplete="off"
              placeholder="请输入密码"
            ></el-input>
          </el-form-item>
          
        </el-form>

        <div class="btn">
          <el-button
            type="primary"
            @keyup.enter.native="submitForm('ruleForm')"
            @click="submitForm('ruleForm')"
            >提交</el-button
          >
        </div>
      </div>
    </div>

    <canvas id="starCanvas"></canvas>
  </div>
</template>

<script>
import { mapActions } from "vuex";
import { login } from "../api/index";
export default {
  data() {
    var checkAge = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        name: "",
        pwd: "",
        code: "",
      },
      imgcode: "",
      rules: {
        name: [{ required: true, validator: checkAge, trigger: "blur" }],
        pwd: [{ required: true, validator: validatePass, trigger: "blur" }],
      },
    };
  },
  created() {
    // this.img();
  },
  mounted() {
    this.starryFun();
  },
  methods: {
    ...mapActions("modules", ["tokenFun", "nameFun"]),
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          login(this.ruleForm).then((res) => {
            console.log(res.data);
            if (res.data.status === 200) {
              this.$message({
                message: res.data.msg,
                type: "success",
                duration: 1000,
              });
              localStorage.setItem("token", res.data.data.token);
              this.tokenFun(res.data.data.token);
              this.nameFun(this.ruleForm.name);
              if (sessionStorage.getItem("toPath")) {
                this.$router.push(`${sessionStorage.getItem("toPath")}`);
                sessionStorage.removeItem("toPath");
              } else {
                this.$router.push("/");
              }
            } else {
              this.$message({
                message: res.data.msg,
                type: "error",
                duration: 1000,
              });
              this.img();
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    starryFun() {
      // 可调参数
      let BACKGROUND_COLOR = "rgba(0,43,54,1)"; // 背景色
      let POINT_NUM = 99; // 屏幕上点的数目
      let POINT_COLOR = "rgba(255,255,255,0.7)"; // 点的颜色
      let LINE_LENGTH = 10000; // 点之间连线长度(的平方)

      // 创建背景画布
      let cvs = document.getElementById("starCanvas");
      cvs.width = window.innerWidth;
      cvs.height = window.innerHeight;
      cvs.style.cssText =
        "\
    position:fixed;\
    top:0px;\
    left:0px;\
    z-index:-1;\
    opacity:1.0;\
    ";
      document.body.appendChild(cvs);

      let ctx = cvs.getContext("2d");

      let startTime = new Date().getTime();

      //随机数函数
      function randomInt(min, max) {
        return Math.floor((max - min + 1) * Math.random() + min);
      }

      function randomFloat(min, max) {
        return (max - min) * Math.random() + min;
      }

      //构造点类
      function Point() {
        this.x = randomFloat(0, cvs.width);
        this.y = randomFloat(0, cvs.height);

        let speed = randomFloat(0.3, 1.4);
        let angle = randomFloat(0, 2 * Math.PI);

        this.dx = Math.sin(angle) * speed;
        this.dy = Math.cos(angle) * speed;

        this.r = 1.2;

        this.color = POINT_COLOR;
      }

      Point.prototype.move = function() {
        this.x += this.dx;
        if (this.x < 0) {
          this.x = 0;
          this.dx = -this.dx;
        } else if (this.x > cvs.width) {
          this.x = cvs.width;
          this.dx = -this.dx;
        }
        this.y += this.dy;
        if (this.y < 0) {
          this.y = 0;
          this.dy = -this.dy;
        } else if (this.y > cvs.height) {
          this.y = cvs.height;
          this.dy = -this.dy;
        }
      };

      Point.prototype.draw = function() {
        ctx.fillStyle = this.color;
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.r, 0, Math.PI * 2);
        ctx.closePath();
        ctx.fill();
      };

      let points = [];

      function initPoints(num) {
        for (let i = 0; i < num; ++i) {
          points.push(new Point());
        }
      }

      let p0 = new Point(); //鼠标
      p0.dx = p0.dy = 0;
      let degree = 2.5;
      document.onmousemove = function(ev) {
        p0.x = ev.clientX;
        p0.y = ev.clientY;
      };
      document.onmousedown = function(ev) {
        degree = 5.0;
        p0.x = ev.clientX;
        p0.y = ev.clientY;
      };
      document.onmouseup = function(ev) {
        degree = 2.5;
        p0.x = ev.clientX;
        p0.y = ev.clientY;
      };
      window.onmouseout = function() {
        p0.x = null;
        p0.y = null;
      };

      function drawLine(p1, p2, deg) {
        let dx = p1.x - p2.x;
        let dy = p1.y - p2.y;
        let dis2 = dx * dx + dy * dy;
        if (dis2 < 2 * LINE_LENGTH) {
          if (dis2 > LINE_LENGTH) {
            if (p1 === p0) {
              p2.x += dx * 0.03;
              p2.y += dy * 0.03;
            } else return;
          }
          let t = (1.05 - dis2 / LINE_LENGTH) * 0.2 * deg;
          ctx.strokeStyle = "rgba(255,255,255," + t + ")";
          ctx.beginPath();
          ctx.lineWidth = 1.5;
          ctx.moveTo(p1.x, p1.y);
          ctx.lineTo(p2.x, p2.y);
          ctx.closePath();
          ctx.stroke();
        }
        return;
      }

      //绘制每一帧
      function drawFrame() {
        cvs.width = window.innerWidth;
        cvs.height = window.innerHeight;
        ctx.fillStyle = BACKGROUND_COLOR;
        ctx.fillRect(0, 0, cvs.width, cvs.height);

        let arr = p0.x == null ? points : [p0].concat(points);
        for (let i = 0; i < arr.length; ++i) {
          for (let j = i + 1; j < arr.length; ++j) {
            drawLine(arr[i], arr[j], 1.0);
          }
          arr[i].draw();
          arr[i].move();
        }

        requestAnimationFrame(drawFrame);
      }

      initPoints(POINT_NUM);
      drawFrame();
    },
  },
  destroyed() {
    let cvs = document.getElementById("starCanvas");
    if (cvs) {
      cvs.parentNode.removeChild(cvs);
    }
  },
};
</script>

<style lang="scss">
.box {
  width: 944px;
  height: 399px;
  background: white;
  display: flex;
  border-radius: 10px 10px 10px 10px;
  .lef {
    width: 511px;
    height: 100%;
    img {
      width: 100%;
      height: 100%;
      display: block;
      border-radius: 10px 0 0 10px;
    }
  }
}
$bg: #283443;
$light_gray: #fff;
$cursor: rgb(0, 0, 0);
$dark_gray: #889aa4;
$light_gray: rgb(0, 0, 0);
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
.imgBox {
  width: 100%;
  display: flex;
}
.Box {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.login-container {
  width: 435px;
  height: 399px;
  background: white;
  box-sizing: border-box;
  padding: 0 20px;
  border-radius: 0 10px 10px 0;

  .logo {
    width: 100%;
    height: 120px;
    display: flex;
    justify-content: center;
    align-items: center;
    background: white;
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
  .img {
    height: 50px;
    width: 120px;
    display: block;
  }

  .el-form-item {
    // border: 1px solid rgba(255, 255, 255, 0.1);
    // background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
    border: 1px solid #ccc;
  }
}
.btn {
  width: 100%;
  button {
    width: 100%;
  }
}
</style>
